package com.boot.mgt.service.impl

import com.boot.core.result.ResultTo
import com.boot.core.util.ManagerKit
import com.boot.mgt.dao.mysql.TaAccidentMapper
import com.boot.mgt.dao.mysql.TaAcdFile2018Mapper
import com.boot.mgt.service.TaAccidentStatisticsService
import com.boot.mgt.service.TaPoliceService
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.HashMap


/**
 * @author rankai on 2018/3/22.
 */
@AutoJsonRpcServiceImpl
@Service
class TaAccidentStatisticsServiceImpl : TaAccidentStatisticsService {

    @Autowired
    lateinit var taAccidentMapper: TaAccidentMapper

    @Autowired
    lateinit var taAcdFile2018Mapper: TaAcdFile2018Mapper

    @Autowired
    private lateinit var taPoliceService: TaPoliceService

    override fun accidentMapAll(startTime: Date, endTime: Date): ResultTo {
        //add by liuzhe @2019-03-26 如果获取3月1日前的数据，则查询从3月1日开始
        var realStartTime = startTime
        if (realStartTime.before(SimpleDateFormat("yyyy-MM-dd").parse("2019-03-01"))){
            realStartTime = SimpleDateFormat("yyyy-MM-dd").parse("2019-03-01")
        }
        val map = HashMap<String, Any?>()
        map.put("startTime", realStartTime)
//        map.put("police_number", if (ManagerKit.isCaptain()) null else ManagerKit.getPoliceNumber())
        var police_number: String? = ManagerKit.getPoliceNumber()
        map.put("police_number", police_number)
        map.put("endTime", endTime)
//        map.put("orgNum", ManagerKit.getOrgNum())
        var policeInfo: MutableMap<String, Any> = taPoliceService.searchPoliceById(police_number)
        map["orgNum"] = taPoliceService.consRegion(policeInfo["region_id"] as String) as String

        val mapValues = taAccidentMapper.accidentMapAll(map)
        val tempList = arrayListOf<Map<String, Any?>>()
        for (mapValue in mapValues) {
            val mapTemp = hashMapOf<String, Any?>()
            for (entry in mapValue) {
                mapTemp[entry.key] = entry.value
                if (mapTemp.size == mapTemp.size) {
                    mapTemp["isOld"] = 0
                }
            }
            tempList.add(mapTemp)
        }
        return ResultTo().apply {
            this["data"] = tempList
        }
    }

    override fun accidentMapAllRpc(startTime: Date, endTime: Date, police_number: String?): ResultTo {
        //add by liuzhe @2019-03-26 如果获取3月1日前的数据，则查询从3月1日开始
        var realStartTime = startTime
        if (realStartTime.before(SimpleDateFormat("yyyy-MM-dd").parse("2019-03-01"))){
            realStartTime = SimpleDateFormat("yyyy-MM-dd").parse("2019-03-01")
        }
        val map = HashMap<String, Any?>()
        map.put("startTime", realStartTime)
//        map.put("police_number", if (ManagerKit.isCaptain()) null else ManagerKit.getPoliceNumber())
        var police_number: String? = police_number
        map.put("police_number", police_number)
        map.put("endTime", endTime)
//        map.put("orgNum", ManagerKit.getOrgNum())
        var policeInfo: MutableMap<String, Any> = taPoliceService.searchPoliceById(police_number)
        map["orgNum"] = taPoliceService.consRegion(policeInfo["region_id"] as String) as String

        val mapValues = taAccidentMapper.accidentMapAll(map)
        val tempList = arrayListOf<Map<String, Any?>>()
        for (mapValue in mapValues) {
            val mapTemp = hashMapOf<String, Any?>()
            for (entry in mapValue) {
                mapTemp[entry.key] = entry.value
                if (mapTemp.size == mapTemp.size) {
                    mapTemp["isOld"] = 0
                }
            }
            tempList.add(mapTemp)
        }
        return ResultTo().apply {
            this["data"] = tempList
        }
    }

    override fun accidentMapOld(startTime: Date, endTime: Date): ResultTo {
        val map = HashMap<String, Any?>()
        map.put("startTime", startTime)
        map.put("endTime", endTime)
        map.put("orgNum", ManagerKit.getOrgNum())

        val mapValues = taAcdFile2018Mapper.accidentMapOld(map)
        val tempList = arrayListOf<Map<String, Any?>>()
        for (mapValue in mapValues) {
            val mapTemp = hashMapOf<String, Any?>()
            for (entry in mapValue) {
                mapTemp[entry.key] = entry.value
                if (entry.key == "type") {
                    when {
                        (entry.value as String).toInt() == 1 -> mapTemp[entry.key] = 3
                        (entry.value as String).toInt() == 2 -> mapTemp[entry.key] = 2
                        (entry.value as String).toInt() == 3 -> mapTemp[entry.key] = 1
                        (entry.value as String).toInt() == 4 -> mapTemp[entry.key] = 4
                    }
                }
                if (mapTemp.size == mapTemp.size) {
                    mapTemp["isOld"] = 1
                    mapTemp["state"] = 3
                    mapTemp["report_id"] = -1
                    mapTemp["deal_type"] = 1
                }
            }
            tempList.add(mapTemp)
        }
        return ResultTo().apply {
            this["data"] = tempList
        }
    }

    override fun accidentMapOldRpc(startTime: Date, endTime: Date, org_num: String?): ResultTo {
        val map = HashMap<String, Any?>()
        map.put("startTime", startTime)
        map.put("endTime", endTime)
        map.put("orgNum", org_num)

        val mapValues = taAcdFile2018Mapper.accidentMapOld(map)
        val tempList = arrayListOf<Map<String, Any?>>()
        for (mapValue in mapValues) {
            val mapTemp = hashMapOf<String, Any?>()
            for (entry in mapValue) {
                mapTemp[entry.key] = entry.value
                if (entry.key == "type") {
                    when {
                        (entry.value as String).toInt() == 1 -> mapTemp[entry.key] = 3
                        (entry.value as String).toInt() == 2 -> mapTemp[entry.key] = 2
                        (entry.value as String).toInt() == 3 -> mapTemp[entry.key] = 1
                        (entry.value as String).toInt() == 4 -> mapTemp[entry.key] = 4
                    }
                }
                if (mapTemp.size == mapTemp.size) {
                    mapTemp["isOld"] = 1
                    mapTemp["state"] = 3
                    mapTemp["report_id"] = -1
                    mapTemp["deal_type"] = 1
                }
            }
            tempList.add(mapTemp)
        }
        return ResultTo().apply {
            this["data"] = tempList
        }
    }


}